// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//
#pragma once

#include <tuple>
#include <type_traits>
#include <unordered_map>
#include <vector>

#include <pollux/common/base/exceptions.h>
#include <pollux/common/meta_programming.h>
#include <pollux/functions/udf_output_string.h>
#include <pollux/type/type.h>
#include <pollux/type/variant.h>

namespace kumo::pollux::core {
    // A simple & efficient container/target for user strings
    struct StringWriter : public UDFOutputString {
        StringWriter() noexcept : storage_{} {
            setData(storage_.data());
        }

        /* implicit */
        StringWriter(StringView /*value*/) {
            POLLUX_NYI();
        }

        void setEmpty() {
            POLLUX_FAIL("setEmpty is not implemented");
        }

        void setNoCopy(StringView /*value*/) {
            POLLUX_FAIL("setNoCopy is not implemented");
        }

        StringWriter(const StringWriter &rh) : storage_{rh.storage_} {
            setData(storage_.data());
            setSize(rh.size());
            setCapacity(rh.capacity());
        }

        StringWriter(StringWriter &&rh) noexcept : storage_{std::move(rh.storage_)} {
            setData(storage_.data());
            setSize(rh.size());
            setCapacity(rh.capacity());
        }

        StringWriter &operator=(const StringWriter &rh) {
            storage_ = rh.storage_;
            reserve(rh.capacity());
            resize(rh.size());
            return *this;
        }

        template<typename T>
        void operator+=(const T &input) {
            append(input);
        }

        void operator+=(const char *input) {
            append(std::string_view(input));
        }

        template<typename T>
        void append(const T &input) {
            auto oldSize = size();
            resize(this->size() + input.size());
            if (input.size() != 0) {
                DKCHECK(data());
                DKCHECK(input.data());
                std::memcpy(data() + oldSize, input.data(), input.size());
            }
        }

        void append(const char *input) {
            append(std::string_view(input));
        }

        template<typename T>
        void copy_from(const T &input) {
            resize(0);
            append(input);
        }

        void copy_from(const char *input) {
            append(std::string_view(input));
        }

        StringWriter &operator=(StringWriter &&rh) noexcept {
            storage_ = std::move(rh.storage_);
            setData(storage_.data());
            setSize(rh.size());
            setCapacity(rh.capacity());
            return *this;
        }

        void reserve(size_t size) override {
            // Resizing the storage not StringWriter size.
            // This allow us to write directly write into storage_.data() and assuring
            // what we wrote wont be overwritten on future resize calls.
            storage_.resize(size);
            setData(storage_.data());
            setCapacity(size);
        }

        /// Not called by the UDF but should be called internally at the end of the
        /// UDF call
        void finalize() {
            storage_.resize(size());
        }

        operator StringView() const {
            return StringView(data(), size());
        }

    private:
        melon::kmstring storage_;
    };
} // namespace kumo::pollux::core
